//
//  USSponsorUsController.m
//  us
//
//  Created by devkarry on 2025/5/6.
//

#import "USSponsorUsController.h"
#import "USSponsorUsHeaderView.h"
#import "USSponsorSuccessView.h"
#import "USChoosePayTypeView.h"
#import "USGoodsModel.h"
#import "PayMethod.h"
@interface USSponsorUsController ()<USChoosePayTypeViewDelegate>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) USSponsorUsHeaderView *header;

@property (nonatomic,strong) USSponsorSuccessView *successView;

@property (nonatomic, strong) USChoosePayTypeView *payTypeView;

@property (nonatomic,strong) NSMutableArray <USGoodsModel *>*goodList;
@property (nonatomic,strong) USGoodsModel  *selectGood;

@property (nonatomic, assign) NSInteger sponsorType;
@end

@implementation USSponsorUsController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupView];
     
    [self requestGoodList];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(PaySuccess) name:@"PaySuccess" object:nil];
}

- (void)setupView{
    self.us_tableView.emptyDataSetSource = nil;
    self.us_tableView.emptyDataSetDelegate = nil;
    
    if (self.us_tableView.tableHeaderView == nil) {
        self.header = [USSponsorUsHeaderView jk_loadInstanceFromNib];

        UIView *contentHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.jk_width, 660)];
        [contentHeader addSubview:self.header];
        [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        self.us_tableView.tableHeaderView = contentHeader;

    }
    
    [self.view addSubview:self.payTypeView];
    CGFloat styleViewH = 260 + self.view.window.safeAreaInsets.bottom;
    [self.payTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_safeAreaLayoutGuideLeft);
        make.right.mas_equalTo(self.view.mas_safeAreaLayoutGuideRight);
        make.bottom.mas_offset(styleViewH);
        make.height.mas_equalTo(styleViewH);
    }];
    
    @weakify(self)
    [[self.header.payAccountBtn1 rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(__kindof UIControl * _Nullable x) {
     
        @strongify(self)
        if (self.goodList.count > 0) {
            [self alertWithGood:self.goodList.firstObject];
        }
        self.sponsorType = 0;
    }];
 
    [[self.header.payAccountBtn2 rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(__kindof UIControl * _Nullable x) {
     
        @strongify(self)
        if (self.goodList.count > 1) {
            [self alertWithGood:self.goodList[1]];
        }
        self.sponsorType = 1;
    }];

     
    [[self.header.payAccountBtn3 rac_signalForControlEvents:UIControlEventTouchUpInside]
     subscribeNext:^(__kindof UIControl * _Nullable x) {
     
        @strongify(self)
        if (self.goodList.count > 2) {
            [self alertWithGood:self.goodList[2]];
        }
        self.sponsorType = 2;
    }];


}

-(NSMutableArray <USGoodsModel *>*)goodList {
    if (_goodList == nil) {
        _goodList = [NSMutableArray array];
    }
    return _goodList;
}

- (USChoosePayTypeView *)payTypeView {
    if (_payTypeView == nil) {
        _payTypeView = [[USChoosePayTypeView alloc]init];
        _payTypeView.delegate = self;
        _payTypeView.hidden = YES;
    }
    return _payTypeView;
}

- (void)requestGoodList {
    [[EMNetWork shareManager] em_post:PayGoodList
                               params:nil
                                  hud:nil
                           showError:YES
                              success:^(EMApiModel *api) {

     
        if (!api.success) {
            return;
        }
 
        NSArray *goodList = [USGoodsModel mj_objectArrayWithKeyValuesArray:api.data[@"goodsList"]];

        [self.goodList addObjectsFromArray:goodList];
        
        if (self.goodList.count > 0) {
            [self.header.payAccountBtn1 setTitle:self.goodList.firstObject.price forState:0];
        }
        if (self.goodList.count > 1) {
            [self.header.payAccountBtn2 setTitle:self.goodList[1].price forState:0];
        }
        if (self.goodList.count > 2) {
            [self.header.payAccountBtn3 setTitle:self.goodList[2].price forState:0];
        }
    } fail:^{
     

    }];
    
}

#pragma mark - USChoosePayTypeViewDelegate
- (void)payTypedidSelectType:(NSInteger)type {
    //0微信  1 支付宝
    [self payWithType:type];

}
- (void)alertWithGood:(USGoodsModel *)goodModel {
    self.selectGood = goodModel;
    
    [USFeedbackTool impactSoft];
    
    [self.payTypeView styleAppear];

}
 
- (void)payWithType:(NSInteger)type{

    NSString *urlStr = @"";
    if (type == 0) {
        urlStr = @"wxV3/app";
    }else if (type == 1) {
        urlStr = @"ali/app";
    }
    
     [[EMNetWork shareManager] em_post:urlStr
                               params:@{@"goodsId":self.selectGood.goodsId}
                                  hud:nil
                           showError:YES
                              success:^(EMApiModel *api) {

 
         if (api.code != 0) {
             return;
         }
  
      
        if (type == 0) {

            WXPayModel *model = [WXPayModel mj_objectWithKeyValues:api.orderInfo];

            [PayMethod WXPayWithOrder:model];
        }else if (type == 1) {
            [PayMethod AliPayWithOrder:api.orderInfo];

        }

    } fail:^{
     

    }];
}

- (void)PaySuccess {
    [USFeedbackTool impactSoft];

    USSponsorSuccessView *successView = [USSponsorSuccessView jk_loadInstanceFromNib];
    successView.sponsorType = self.sponsorType;
    UIView *contentHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [MOAlertBaseView alertWidth], 170)];
    [contentHeader addSubview:successView];
    [successView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    MOAlertBaseView *alert = [[MOAlertBaseView alloc]initWithTitle:@"谢谢您的投喂～" detailTitle:nil contentView:contentHeader leftButtonImageName:@" " rightButtonImageName:nil autoDissmiss:YES tapDissmiss:YES];
 
 
    [alert show];
}

@end
